package com.eduspace.eduplatform.wares.utils;

import com.github.pagehelper.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author tbc tianbencai@e-eduspace.com
 * @version 1.0 {2016年3月22日 上午9:15:38}
 */
public class SqlMap<K, V> {
	private static Logger log = LoggerFactory.getLogger(SqlMap.class);

	/**
	 * 
	 * @param from
	 * @return
	 * @author tbc tianbencai@e-eduspace.com
	 * @version 1.0 {2016年3月22日 上午11:42:58}
	 * @since jdk-1.7
	 */
	public static Map<String, Object> map(Map<String, Object> from) {
		if (null == from || from.size() == 0) {
			return from;
		}
		Map<String, Object> to = Maps.newHashMap();
		Set<String> keys = from.keySet();
		for (String k : keys) {
			if (null != k) {
				StringBuilder sb = new StringBuilder();
				boolean f = true;
				for (String s : k.split("_")) {
					if (f) {
						sb.append(s);
						f = false;
					} else {
						sb.append(s.substring(0, 1).toUpperCase());
						sb.append(s.substring(1));
					}
				}
				to.put(sb.toString(), from.get(k));
			} else {
				to.put(k, from.get(k));
			}
		}
		return to;
	}

	public static List<Map<String, Object>> map(List<Map<String, Object>> fromList) {
		if (null == fromList || fromList.size() == 0) {
			return fromList;
		}
		List<Map<String, Object>> to = new ArrayList<>();
		for (Map<String, Object> from : fromList) {
			to.add(map(from));
		}
		return to;
	}

	public static PageWrapper<Map<String, Object>> map(Page<Map<String, Object>> page) {
		if (null == page) {
			log.error("分页page是空，肯定是程序出错了，检查检查，没法玩了");
			return null;
		}
		List<Map<String, Object>> list = page.getResult();
		List<Map<String, Object>> rlist = Lists.newArrayList();
		if (null != list && list.size() > 0) {
			rlist = map(list);
		}
		PageWrapper<Map<String, Object>> wrapper = new PageWrapper<>(page);
		wrapper.setList(rlist);
		return wrapper;
	}

	@Deprecated
	public static Page<Map<String, Object>> mapPage(Page<Map<String, Object>> fromList) {
		if (null == fromList || fromList.size() == 0) {
			return fromList;
		}
		Page<Map<String, Object>> to = fromList;
		for (Map<String, Object> from : fromList) {
			to.add(map(from));
		}
		return to;
	}

}
